import { api } from './api';

// 用户接口定义
export interface User {
  id: string;
  username: string;
  email: string;
  role: string;
  avatar?: string;
  createdAt: string;
  updatedAt: string;
}

// 登录请求参数
export interface LoginParams {
  username: string;
  password: string;
}

// 登录响应
export interface LoginResponse {
  token: string;
  user: User;
}

// 用户服务类
export class UserService {
  // 登录
  static async login(params: LoginParams): Promise<LoginResponse> {
    return api.post('/auth/login', params);
  }

  // 登出
  static async logout(): Promise<void> {
    return api.post('/auth/logout');
  }

  // 获取当前用户信息
  static async getCurrentUser(): Promise<User> {
    return api.get('/users/me');
  }

  // 获取用户列表
  static async getUsers(page = 1, pageSize = 10): Promise<{ data: User[]; total: number }> {
    return api.get('/users', { page, pageSize });
  }

  // 获取单个用户信息
  static async getUserById(id: string): Promise<User> {
    return api.get(`/users/${id}`);
  }

  // 创建用户
  static async createUser(userData: Partial<User>): Promise<User> {
    return api.post('/users', userData);
  }

  // 更新用户
  static async updateUser(id: string, userData: Partial<User>): Promise<User> {
    return api.put(`/users/${id}`, userData);
  }

  // 删除用户
  static async deleteUser(id: string): Promise<void> {
    return api.delete(`/users/${id}`);
  }
}

export default UserService;